home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagd_f.zip / DATATYPE.SWG / 0015_Double Words.pas < prev    next >
Pascal/Delphi Source File  |  1994-01-27  |  3KB  |  114 lines

  1. {
  2. > I'm trying to read a record from a file of byte. One of the variables in
  3. > read is the record is a 4 byte unsigned integer (DWORD). Since the
  4. > filetype doesn't allow me to read a dword at once I have to construct it
  5. > myself.
  6. > Could somebody please tell me how to construct my dword?
  7.  
  8. Type
  9.   DWORD = record
  10.     case byte of
  11.       0 : (Full : longint);
  12.       1 : (HiWord, LoWord : word);
  13.       2 : (Hw_HiByte, Hw_LoByte, Lw_HiByte, Lw_LoByte : byte);
  14.       3 : (FourBytes : array[0..3] of byte);
  15.     end;
  16.  
  17. Here is an example:
  18. }
  19.  
  20. {$A+,B-,D+,E-,F+,G+,I+,L+,N+,O+,P+,Q+,R+,S+,T+,V-,X+,Y+}
  21. {$M 1024,0,655360}
  22. uses
  23.   crt;
  24.  
  25. Type
  26.   DWord = record
  27.     case byte of
  28.       0 : (Full : longint);
  29.       1 : (HiWord, LoWord : word);
  30.       2 : (Hw_HiByte, Hw_LoByte, Lw_HiByte, Lw_LoByte : byte);
  31.       3 : (FourBytes : array[0..3] of byte);
  32.       4 : (TwoWords : array[0..1] of word);
  33.     end;
  34.  
  35. var
  36.         F       : file of longint;
  37.   B       : file of byte;
  38.   MyDword : Dword;
  39.   MyLong  : longint;
  40.   MyWord  : word;
  41.   MyByte,
  42.   Index   : byte;
  43.  
  44. begin
  45.         clrscr;
  46.         assign(F, 'MyLong.dat');
  47.   rewrite(F);
  48.   MyLong := $12345678;
  49.   write(F, MyLong);
  50.   MyLong := 0;
  51.   Close(F);
  52.   assign(B, 'MyLong.dat');
  53.   reset(B);
  54.   Seek(B, 0);  { Go back to first record in file}
  55.   for Index := 0 to 3 do
  56.                 read(B, MyDword.Fourbytes[Index]);
  57.   writeln($12345678);
  58.         writeln(MyDword.Full);
  59.   writeln;
  60.   writeln(MyDword.HiWord);
  61.   writeln(MyDword.LoWord);
  62.         writeln;
  63.   writeln(MyDword.Hw_HiByte);
  64.   writeln(MyDword.Hw_LoByte);
  65.   writeln(MyDword.Lw_HiByte);
  66.   writeln(MyDword.Lw_LoByte);
  67.         writeln;
  68.   for Index := 0 to 3 do
  69.           writeln(MyDword.FourBytes[Index]);
  70.   writeln;
  71.   for Index := 0 to 1 do
  72.           writeln(MyDword.TwoWords[Index]);
  73.  
  74.   Close(B);
  75.   reset(F);
  76.   while keypressed do readkey;
  77.         readkey;
  78.   Seek(F, 0);  { Go back to first record in file}
  79.         read(F, MyDword.Full);
  80.   ClrScr;
  81.   writeln($12345678);
  82.         writeln(MyDword.Full);
  83.   writeln;
  84.   writeln(MyDword.HiWord);
  85.   writeln(MyDword.LoWord);
  86.         writeln;
  87.   writeln(MyDword.Hw_HiByte);
  88.   writeln(MyDword.Hw_LoByte);
  89.   writeln(MyDword.Lw_HiByte);
  90.   writeln(MyDword.Lw_LoByte);
  91.         writeln;
  92.   for Index := 0 to 3 do
  93.           writeln(MyDword.FourBytes[Index]);
  94.   writeln;
  95.   for Index := 0 to 1 do
  96.           writeln(MyDword.TwoWords[Index]);
  97.   close(F);
  98.   while keypressed do readkey;
  99.         readkey;
  100. end.
  101.  
  102. {
  103. Compiled and Tested with BP 7.x
  104.  
  105. It will, write a file of Longint, write 12345678 Hex to it, read it as a
  106. file of byte, display most representation of it, then close it and
  107. reopen it as a file of LongInt again read one longint and again display
  108. the representations of it.
  109.  
  110. PS. There is a pause after the first display (Read as a file of bytes),
  111. any key presents the second display (Read as a file of bytes), and
  112. another pause to allow you to see that it does display the same thing.
  113. Any key then terminates the program.
  114. }